package thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class Demo29 {
    public static void main(String[] args) {
        //BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(1000);
        MyBlockingQueue queue = new MyBlockingQueue(1000);
        //生产者
        Thread t1 = new Thread(() -> {
            int i = 0;
            while(true) {
                try {
                    queue.put(i);
                    System.out.println("生成生产元素: " + i);
                    i++;
                    //生产速度减慢，消费加快
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        //消费者
        Thread t2 = new Thread(() -> {
            while(true) {
                try {
                    int i = (int) queue.take();
                    System.out.println("生成生产元素: " + i);

                    //减慢消费者消费速度
                    //Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        t1.start();
        t2.start();
    }
}
